package com.party.core.service.workflow.impl;

import com.party.common.paging.Page;
import com.party.common.utils.StringUtils;
import com.party.core.dao.read.workflow.BusinessInstanceReadDao;
import com.party.core.dao.write.workflow.BusinessInstanceWriteDao;
import com.party.core.model.BaseModel;
import com.party.core.model.workflow.BusinessInstance;
import com.party.core.service.workflow.IBusinessInstanceService;
import org.apache.commons.collections.map.HashedMap;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.*;

/**
 * @author: Administrator.
 * @Description: 流程实例
 * @Date:Created in 2017/10/24 0024 下午 16:13.
 */
@Service
public class BusinessInstanceService implements IBusinessInstanceService {
    @Autowired
    private BusinessInstanceReadDao businessInstanceReadDao;
    @Autowired
    private BusinessInstanceWriteDao businessInstanceWriteDao;

    @Override
    public String insert(BusinessInstance t) {
        BaseModel.preInsert(t);
        boolean result = businessInstanceWriteDao.insert(t);
        if (result) {
            return t.getId();
        }
        return null;
    }

    @Override
    public boolean update(BusinessInstance t) {
        if (null == t) {
            return false;
        }
        t.setUpdateDate(new Date());
        return businessInstanceWriteDao.update(t);
    }

    @Override
    public boolean deleteLogic(String id) {
        if (StringUtils.isBlank(id)) {
            return false;
        }

        return businessInstanceWriteDao.deleteLogic(id);
    }

    @Override
    public boolean delete(String id) {
        if (StringUtils.isBlank(id)) {
            return false;
        }

        return businessInstanceWriteDao.delete(id);
    }

    @Override
    public boolean batchDeleteLogic(Set<String> ids) {
        if (CollectionUtils.isEmpty(ids)) {
            return false;
        }

        return businessInstanceWriteDao.batchDeleteLogic(ids);
    }

    @Override
    public boolean batchDelete(Set<String> ids) {
        if (CollectionUtils.isEmpty(ids)) {
            return false;
        }

        return businessInstanceWriteDao.batchDelete(ids);
    }

    @Override
    public BusinessInstance get(String id) {
        return businessInstanceReadDao.get(id);
    }

    @Override
    public List<BusinessInstance> listPage(BusinessInstance t, Page page) {
        return businessInstanceReadDao.listPage(t, page);
    }

    @Override
    public List<BusinessInstance> list(BusinessInstance t) {
        return businessInstanceReadDao.listPage(t, null);
    }

    @Override
    public List<BusinessInstance> batchList(Set<String> ids, BusinessInstance t, Page page) {
        if (CollectionUtils.isEmpty(ids)) {
            return Collections.EMPTY_LIST;
        }

        return businessInstanceReadDao.batchList(ids, new HashedMap(), page);
    }

    @Override
    public BusinessInstance findByTargetId(String projectId) {
        BusinessInstance instance = new BusinessInstance();
        instance.setTargetId(projectId);
        return businessInstanceReadDao.getUnique(instance);
    }

    @Override
    public List<Map<String, Object>> webListPage(Map<String, Object> params, Page page) {
        return businessInstanceReadDao.webListPage(params, page);
    }
}
